package com.zhb86.kuaipai.ui.home;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.dueeeke.videoplayer.player.VideoView;
import com.dueeeke.videoplayer.player.VideoViewManager;
import com.superyee.commonlib.constants.StaticConstant;
import com.superyee.commonlib.ui.BaseFragment;
import com.superyee.commonlib.utils.Tag;
import com.superyee.commonlib.videoplayer.cache.PreloadManager;
import com.superyee.commonlib.videoplayer.controller.TikTokController;
import com.zhb86.kuaipai.R;
import com.zhb86.kuaipai.bean.VideoBean;

/**
 * A simple {@link Fragment} subclass.
 */
public class FVideoPlay extends BaseFragment {

    /**
     * @param videoBean
     * @param recyclerPosition
     * @param recommendPage    是否是推荐列表  推荐列表包含手势打开用户详情页面
     * @return
     */
    public static FVideoPlay newInstance(VideoBean videoBean, int recyclerPosition, boolean recommendPage) {
        Bundle data = new Bundle();
        data.putParcelable(StaticConstant.Extra.DATA, videoBean);
        data.putBoolean(StaticConstant.Extra.TYPE, recommendPage);
        data.putInt(StaticConstant.Extra.ID, recyclerPosition);
        FVideoPlay fragment = new FVideoPlay();
        fragment.setArguments(data);
        return fragment;
    }

    private VideoView mVideoView;
    private VideoBean mVideo;
    private int mRecyclerPosition;
    private boolean mRecommendPage;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video_play;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mVideoView = view.findViewById(R.id.videoView);
        mVideoView.setLooping(true);
        mVideoView.setScreenScaleType(VideoView.SCREEN_SCALE_CENTER_CROP);
        if (savedInstanceState != null) {
            mVideo = savedInstanceState.getParcelable(StaticConstant.Extra.DATA);
            mRecyclerPosition = savedInstanceState.getInt(StaticConstant.Extra.ID);
            mRecommendPage = savedInstanceState.getBoolean(StaticConstant.Extra.TYPE);
        }
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putBoolean(StaticConstant.Extra.TYPE, mRecommendPage);
        outState.putParcelable(StaticConstant.Extra.DATA, mVideo);
        outState.putInt(StaticConstant.Extra.ID, mRecyclerPosition);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void initData() {
        if (getArguments() != null) {
            mVideo = getArguments().getParcelable(StaticConstant.Extra.DATA);
            mRecyclerPosition = getArguments().getInt(StaticConstant.Extra.ID);
            mRecommendPage = getArguments().getBoolean(StaticConstant.Extra.TYPE);
        }
        if (mVideo != null) {
            TikTokController controller = new TikTokController(baseActivity, mRecommendPage);
            if (mRecommendPage) {
                controller.setOnNextPageListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(baseActivity, ATUserHomePage.class));
                    }
                });
            }
            mVideoView.setVideoController(controller);
            //开始预加载
            if (mVideo.getUrl() != null) {
                PreloadManager.getInstance(baseActivity).addPreloadTask(mVideo.getUrl(), mRecyclerPosition);
//                String playUrl = PreloadManager.getInstance(baseActivity).getPlayUrl(mVideo.getUrl());
//                mVideoView.setUrl(playUrl);
            }
            Glide.with(this)
                    .load(mVideo.getThumb())
                    .placeholder(android.R.color.white)
                    .into(controller.getThumb());
        }
    }

    public void startPlay() {
        if (mVideo == null || mVideoView == null || mVideo.getUrl() == null)
            return;
        VideoViewManager.instance().add(mVideoView, mRecommendPage ? Tag.RECOMMEND : Tag.FOLLOW);
        if (mVideoView.getCurrentPlayState() == VideoView.STATE_PAUSED || mVideoView.getCurrentPlayState() == VideoView.STATE_PLAYING) {
            mVideoView.resume();
            return;
        }
        String playUrl = PreloadManager.getInstance(baseActivity).getPlayUrl(mVideo.getUrl());
        mVideoView.setUrl(playUrl);
        mVideoView.start();
    }

    public void pause() {
        if (mVideoView != null)
            mVideoView.pause();
    }


    @Override
    public void onDetach() {
        mVideoView.release();
        //取消预加载
        if (mVideo != null || mVideoView != null || mVideo.getUrl() != null) {
            PreloadManager.getInstance(baseActivity).removePreloadTask(mVideo.getUrl());
        }
        super.onDetach();
    }


    @Override
    protected void addListener() {

    }

    @Override
    protected void removeListener() {

    }

    @Override
    public void onReceiverNotify(int action, Object obj, int statusCode) {

    }
}
